﻿#include "toolbarwidget.h"
#include "ui_toolbarwidget.h"
#include "browsermainwindow.h"

ToolBarWidget::ToolBarWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ToolBarWidget)
{
    ui->setupUi(this);

}

ToolBarWidget::~ToolBarWidget()
{
    delete ui;
}

//// 键盘监听，回车控制加载
//void ToolBarWidget::keyReleaseEvent(QKeyEvent *event)
//{
//    qDebug() << "键盘事件触发";
//    if(event->key() == Qt::Key_Enter){
//        // 加载当前url输入框的内容
//        BrowserMainWindow *mainWindow = qobject_cast<BrowserMainWindow*>(window());
//        const QString url = this->ui->url_edit->text();
//        mainWindow->tabWidget()->currentTab->loadUrl(QUrl(url));
//    }
//}

const QString& ToolBarWidget::getUrl()
{
    return this->ui->url_edit->text();
}

void ToolBarWidget::setUrl(const QString &url)
{
    this->ui->url_edit->setText(url);
}

void ToolBarWidget::paintEvent(QPaintEvent *event)
{
    //
    qDebug() << "工具栏重绘" << size();
    this->ui->url_edit->setMinimumWidth(size().width() - 650);
//    this->ui->url_edit->resize(size().width() - 650, size().height());
}

void ToolBarWidget::on_close_btn_clicked()
{
    // 关闭窗口
    BrowserMainWindow *mainWindow = qobject_cast<BrowserMainWindow*>(window());
    mainWindow->close();
}


void ToolBarWidget::on_max_min_btn_clicked()
{
    // 最大化最小化窗口
    BrowserMainWindow *mainWindow = qobject_cast<BrowserMainWindow*>(window());
    if(mainWindow->isMaximized()){
        this->ui->max_min_btn->setIcon(QIcon(QStringLiteral(":/app_resource/max.png")));
        mainWindow->showNormal();
    }else{
        this->ui->max_min_btn->setIcon(QIcon(QStringLiteral(":/app_resource/mid.png")));
        mainWindow->showMaximized();
    }
}


void ToolBarWidget::on_min_btn_clicked()
{
    // 最小化窗口
    BrowserMainWindow *mainWindow = qobject_cast<BrowserMainWindow*>(window());
    mainWindow->showMinimized();
}


void ToolBarWidget::on_add_btn_clicked()
{
    // 新建标签页
    BrowserMainWindow *mainWindow = qobject_cast<BrowserMainWindow*>(window());
    mainWindow->tabWidget()->createTab();
}


void ToolBarWidget::on_collect_btn_clicked()
{
    // 收藏当前链接, TODO
    BrowserMainWindow *mainWindow = qobject_cast<BrowserMainWindow*>(window());
    CustomWebView* view = mainWindow->tabWidget()->currentTab->view_widget;
    UrlInfo urlInfo(view->title().toStdString().c_str(), "", view->url().toString().toStdString().c_str());
    mainWindow->addFavirote(urlInfo);

    NotifyManager *manager = new NotifyManager(nullptr);
    manager->setMaxCount(5);
    manager->setDisplayTime(5000);
    manager->setNotifyWndSize(500, 200);
    manager->notify("提示消息", "网页收藏完成");
}


void ToolBarWidget::on_home_btn_clicked()
{
    // 回到浏览器主页
    BrowserMainWindow *mainWindow = qobject_cast<BrowserMainWindow*>(window());
    ConfigManager &instance = ConfigManager::getInstance();
    QString url = QString(instance.getDefaultIndex().c_str());
    mainWindow->tabWidget()->currentTab->loadUrl(QUrl(url));
}


void ToolBarWidget::on_refresh_btn_clicked()
{
    // 刷新当前页面
    BrowserMainWindow *mainWindow = qobject_cast<BrowserMainWindow*>(window());
    mainWindow->tabWidget()->currentTab->refresh();
}


void ToolBarWidget::on_back_btn_clicked()
{
    // 后退
    BrowserMainWindow *mainWindow = qobject_cast<BrowserMainWindow*>(window());
    mainWindow->tabWidget()->currentTab->view_widget->back();
}


void ToolBarWidget::on_forward_btn_clicked()
{
    // 前进
    BrowserMainWindow *mainWindow = qobject_cast<BrowserMainWindow*>(window());
    mainWindow->tabWidget()->currentTab->view_widget->forward();
}


void ToolBarWidget::on_url_edit_returnPressed()
{
    // 加载当前url输入框的内容
    BrowserMainWindow *mainWindow = qobject_cast<BrowserMainWindow*>(window());
    const QString url = this->ui->url_edit->text();
    mainWindow->tabWidget()->currentTab->loadUrl(QUrl(url));
}

